package cool.houge.pig.infra.config;

import com.auth0.jwt.algorithms.Algorithm;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.Duration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.resources.ConnectionProvider;

/**
 * 基础配置.
 *
 * @author KK (kzou227@qq.com)
 */
@Configuration
public class BasisConfiguration {

  @Bean
  public Jackson2JsonEncoder jackson2JsonEncoder(ObjectMapper objectMapper) {
    return new Jackson2JsonEncoder(objectMapper);
  }

  @Bean
  public Jackson2JsonDecoder jackson2JsonDecoder(ObjectMapper objectMapper) {
    return new Jackson2JsonDecoder(objectMapper);
  }

  @Bean
  public WebClient webhookWebClient() {
    var cp = ConnectionProvider.builder("webhook-conn").build();
    var httpClient = HttpClient.create(cp).responseTimeout(Duration.ofSeconds(10));
    return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)).build();
  }

  @Bean
  public PasswordEncoder passwordEncoder() {
    return NoOpPasswordEncoder.getInstance();
  }

  @Bean
  public Algorithm algorithm() {
    return Algorithm.HMAC512(
        "]>N2~,pw=DTj~w<M!xkP=J,m(!6vni2iR4('|&{Z=4jh8anny61>WCr&K>~d~'&2OFJH$j{;i|X?x<p%Kq!<h$yp(n8C]>ZOtwfzIfvvt=-]E$_rW>?YL_')KJV;}Uj*");
  }
}
